
#include "iostream"
#include <bitset>
#include <vector>
#include "f_extern.h"

using namespace std;

int ls_dir()
{
	char name[10];
	int num_next = 0;
	int num_chld;
	int cur_block_size,last_block_size;
	int tmp_size;
	header head;
	header chld_head;
	string path;
	string chld_path;
	FILE *ptr;
	FILE *fd;
	time_t ltime;
	int tmp_variant = -1;
	int base_block, base_block_prev;
	char* data;

	base_block = base_block_prev =1;
	for(vector<string>::iterator i=dir_set.begin();i!=dir_set.end();i++)
	{
		base_block_prev = base_block;
		sprintf(name,"./.%d",base_block);	
		path = root_dir; path.append(name);
		fd = fopen(path.data(),"rb");
		fread(&head,sizeof(header),1,fd);
		fclose(fd);
		
		num_next = head.data_block;	
		do
		{
			sprintf(name,"./.%d",num_next);
			path = root_dir; path.append(name);
			fd = fopen(path.data(),"rb");
			for(int j=0;j<head.size && j<block_size-(sizeof(int)); j+=sizeof(int))
			{
				
				fread(&num_chld,sizeof(int),1,fd);
				sprintf(name,"./.%d",num_chld);
				chld_path = root_dir; chld_path.append(name);
				ptr = fopen(chld_path.data(),"rb");
				fread(&chld_head,sizeof(header),1,ptr);
				fclose(ptr);
				if(strcmp((*i).data(),chld_head.name)==0)
				{
					
					if(i!=dir_set.end()-1)
					{
						if(chld_head.type==0)
						{
							base_block = num_chld;
							tmp_variant = num_chld;
							if(chld_head.type == root_dir_type)
								break;
						}
					}
					else
					{
						
						base_block = num_chld;
						tmp_variant = num_chld;
						if(chld_head.type == root_dir_type)
							break;
					}
					
				}
				
			}
			if(base_block_prev == base_block){
			
				head.size -= (block_size-sizeof(int));
				fseek(fd,-sizeof(int),SEEK_END);
				fread(&num_next,sizeof(int),1,fd);
				fclose(fd);
			}
			else
			{
				fclose(fd);
				break;
				
			}
		}while(num_next!=0);
		
		if(base_block_prev == base_block && tmp_variant == -1)
		{
#ifdef PRINT_MSG
			printf("[ls]:Directory/file isn't found\n");
#endif
			return 3;
		}		
		else
		{
			base_block = tmp_variant;
		}
	}
	sprintf(name,"./.%d",base_block);	
	path = root_dir; path.append(name);
	fd = fopen(path.data(),"rb");
	fread(&head,sizeof(header),1,fd);
	fclose(fd);
	if(head.type==0)
	{
		for(vector<string>::iterator i=dir_set.begin();i!=dir_set.end();i++)
		{
			printf("/%s",(*i).data());
		}
		printf("\n");
		num_next = head.data_block;
		do
		{
			sprintf(name,"./.%d",num_next);
			path = root_dir; path.append(name);
			fd = fopen(path.data(),"rb");
			for(int j=0;j<head.size && j<block_size-(sizeof(int)); j+=sizeof(int))
			{
				
				fread(&num_chld,sizeof(int),1,fd);
				sprintf(name,"./.%d",num_chld);
				chld_path = root_dir; chld_path.append(name);
				ptr = fopen(chld_path.data(),"rb");
				fread(&chld_head,sizeof(header),1,ptr);
				fclose(ptr);
				if(chld_head.type == 0)
				{
					ltime = chld_head.last_time;
					data =ctime(&ltime);
					printf("\t-d /%s \t%s\n",chld_head.name,data);
					delete data;
				}
				else
				{
					ltime = chld_head.last_time;
					data =ctime(&ltime);
					printf("\t-f /%s \tSize = %d blocks\t%s\n",chld_head.name,chld_head.size_in_block,data);
					delete data;
				}
			}	
			head.size -= (block_size-sizeof(int));
			fseek(fd,-sizeof(int),SEEK_END);
			fread(&num_next,sizeof(int),1,fd);
			fclose(fd);
		
		}while(num_next!=0);
	}
	else if(head.type == 1)
	{
		ltime = head.last_time;
		data =ctime(&ltime);
		printf("\tSize %d blocks\n",head.size_in_block);
		printf("\tDate %s \n",data);
		delete data;
	}
	return 0;
}

int main(int argc, char* argv[])
{	
	char* _f_ = "[ls]";
	int next_dir_num = 0;
	int i;
	FILE* config = NULL; 
	FILE* block = NULL;
	string file_name;
	
	header dir;
	string dir_path;
	if (argc != 3){
#ifdef PRINT_MSG
		printf("%s: Use  ls root path\n",_f_); 
#endif	
		return 2;
	}
	
#ifdef _DEBUG
	printf("%s: ROOT DIR %s\n",_f_,argv[1]);
#endif
		root_dir = argv[1];
		make_build_map(1);
	
		dir_path = argv[2];
		parse_path(dir_path);
		
		ls_dir();	

	
	return 0;
}


